package com.zkh.myframe.database.mapper;

import com.zkh.myframe.core.FrameConfigure;
import com.zkh.myutils.utils.StringUtils;

/**
 * Mapper实体
 * @author zkh
 */
public class MapperEntity {
	//select语句
	private SQLMapper select = new SQLMapper();
	//update语句
	private SQLMapper update = new SQLMapper();
	//insert语句
	private SQLMapper insert = new SQLMapper();
	//delete语句
	private SQLMapper delete = new SQLMapper();
	//sql片段
	private SQLMapper sql = new SQLMapper();

	/**
	 * 添加Select实体
	 * @param id 配置的ID
	 * @param sqlEntity SQL实体
	 */
	public void addSelect(String id, SQLEntity sqlEntity) {
		select.setSQLEntity(id, sqlEntity.renderOperType("select"));
	}
	
	/**
	 * 添加Update实体
	 * @param id 配置的ID
	 * @param sqlEntity SQL实体
	 */
	public void addUpdate(String id, SQLEntity sqlEntity) {
		update.setSQLEntity(id, sqlEntity.renderOperType("update"));
	}
	
	/**
	 * 添加Insert实体
	 * @param id 配置的ID
	 * @param sqlEntity SQL实体
	 */
	public void addInsert(String id, SQLEntity sqlEntity) {
		insert.setSQLEntity(id, sqlEntity.renderOperType("insert"));
	}
	
	/**
	 * 添加Delete实体
	 * @param id 配置的ID
	 * @param sqlEntity SQL实体
	 */
	public void addDelete(String id, SQLEntity sqlEntity) {
		delete.setSQLEntity(id, sqlEntity.renderOperType("delete"));
	}
	
	/**
	 * 添加SQL实体
	 * @param id 配置的ID
	 * @param sqlEntity SQL实体
	 */
	public void addSQL(String id, SQLEntity sqlEntity) {
		sql.setSQLEntity(id, sqlEntity);
	}
	
	/**
	 * 获取Select实体
	 * @param id 配置的ID
	 */
	public SQLEntity getSelect(String id) {
		return select.getSQLEntity(id);
	}
	
	/**
	 * 获取Update实体
	 * @param id 配置的ID
	 */
	public SQLEntity getUpdate(String id) {
		return update.getSQLEntity(id);
	}
	
	/**
	 * 获取Insert实体
	 * @param id 配置的ID
	 */
	public SQLEntity getInsert(String id) {
		return insert.getSQLEntity(id);
	}
	
	/**
	 * 获取Delete实体
	 * @param id 配置的ID
	 */
	public SQLEntity getDelete(String id) {
		return delete.getSQLEntity(id);
	}
	
	/**
	 * 获取SQL实体
	 */
	public SQLEntity getSQL(String id) {
		return sql.getSQLEntityOrDefault(id, new SQLEntity(null, null));
	}
	
	/**
	 * 从全局Mapper中获取SQL实体。即跨命名空间获取SQL标签
	 * @param fullID 完整ID值
	 */
	static SQLEntity getGlobalSQLEntity(String fullID) {
		//空
		if(StringUtils.isEmpty(fullID)) {
			return new SQLEntity(null, null);
		}
		//点的位置
		int idx = fullID.lastIndexOf(".");
		//检查fullID的格式
		if(idx<1 || idx==fullID.length()-1) {
			throw new MapperParseException("从全局Mapper中获取SQL实体时指定的refid（" + fullID + "）格式错误");
		}
		//返回，拆分命名空间和ID
		return getGlobalSQLEntity(fullID.substring(0, idx), fullID.substring(idx+1));
	}
	
	/**
	 * 从全局Mapper中获取SQL实体。即跨命名空间获取SQL标签
	 * @param namespace 命名空间
	 * @param id SQL标签的ID
	 */
	public static SQLEntity getGlobalSQLEntity(String namespace, String id) {
		//返回对应SQLEntity
		return FrameConfigure.getMapperMap().getOrDefault(namespace, new MapperEntity()).getSQL(id);
	}
	
	/**
	 * 查询select、update、insert、delete对象
	 */
	public static SQLEntity getOperateSQLEntity(String namespace, String id) {
		//Mapper
		MapperEntity mapper = FrameConfigure.getMapperMap().get(namespace);
		//不存在Mapper
		if(mapper==null) {
			throw new MapperException("没有对应的Mapper");
		}
		//默认sQL
		SQLEntity sql = mapper.getSelect(id);
		//为空
		if(sql==null) {
			sql = mapper.getUpdate(id);
		}
		//为空
		if(sql==null) {
			sql = mapper.getInsert(id);
		}
		//为空
		if(sql==null) {
			sql = mapper.getDelete(id);
		}
		return sql;
	}
}
